package com.willren;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class AvtSetting extends FrameActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		
		final WifiManagerEx wifi = WifiManagerEx.getWifiManagerExInstance(AvtSetting.this);
		
		EditText apName = (EditText) findViewById(R.id.edit_ap_name);
		if (null != apName) {
			apName.setText(wifi.getApName());
			apName.addTextChangedListener(new TextWatcher() {
				@Override
				public void afterTextChanged(Editable s) {
					wifi.setApName(s.toString());
				}
				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
				}
				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
				}
			});
		}
		
		EditText apPassword = (EditText) findViewById(R.id.edit_ap_password);
		if (null != apPassword) {
			apPassword.setText(wifi.getWepKey());
			apPassword.addTextChangedListener(new TextWatcher() {
				@Override
				public void afterTextChanged(Editable s) {
					wifi.setWepKey(s.toString());
				}
				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
				}
				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
				}
			});
		}
		
		View securityLayout = findViewById(R.id.layout_security_enable);
		if (null != securityLayout) {
			if (false == wifi.isEncrypted()) {
				securityLayout.setVisibility(View.GONE);
			}
		}
		
		ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
		if (null != toggleButton) {
			if (wifi.isEncrypted()) {
				toggleButton.setChecked(true);
				if (null != securityLayout) {
					securityLayout.setVisibility(View.VISIBLE);
				}
			}
			
			final View finalSecurityLayout = securityLayout;
			toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (null != finalSecurityLayout) {
						if (isChecked) {
							finalSecurityLayout.setVisibility(View.VISIBLE);
						}
						else {
							finalSecurityLayout.setVisibility(View.GONE);
						}
					}
					
					wifi.setEncrypted(isChecked);
				}
			});
		}
		
		if (null != wifi) {
			if (wifi.isWifiApStarted()) {
				if (null != apName) {
					apName.setEnabled(false);
				}
				if (null != apPassword) {
					apPassword.setEnabled(false);
				}
				if (null != toggleButton) {
					toggleButton.setEnabled(false);
				}
			}
			else {
				if (null != apName) {
					apName.setEnabled(true);
				}
				if (null != apPassword) {
					apPassword.setEnabled(true);
				}
				if (null != toggleButton) {
					toggleButton.setEnabled(true);
				}
			}
		}
	}
}